#!/usr/bin/env python
# encoding: utf-8
'''
@author: Excelsiorly
@license: (C) Copyright 2021, All Rights Reserved.
@contact: excelsiorly@qq.com
@file: 0015.py
@time: 2021/9/14 14:46
@desc:
'''
# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def mirrorTree(self, root: TreeNode) -> TreeNode:
        if not root:
            return root

        # 将左子树镜像
        left = self.mirrorTree(root.left)
        # 将右子树镜像
        right = self.mirrorTree(root.right)
        root.left, root.right = right, left
        return root

def buildTree()->TreeNode:
    root = TreeNode(4)
    root.left = TreeNode(2)
    root.right = TreeNode(7)
    root.left.left = TreeNode(1)
    root.left.right = TreeNode(3)
    root.right.left = TreeNode(6)
    root.right.right = TreeNode(9)

    return root

if __name__ == '__main__':
    sl = Solution()
    inp = buildTree()
    r = sl.mirrorTree(inp)
    print(r)